package com.zueb.web.servlet;

import com.zueb.pojo.OutApply;
import com.zueb.pojo.User;
import com.zueb.service.OutApplyService;
import com.zueb.service.UserService;
import com.zueb.service.impl.OutApplyServiceImpl;
import com.zueb.service.impl.UserServiceImpl;
import com.zueb.util.ResponseWriterUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "OutApplyServlet", value = "/OutApplyServlet")
public class OutApplyServlet extends HttpServlet {
    private OutApplyService outApplyService = new OutApplyServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转发请求到doPost
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转码
        request.setCharacterEncoding("UTF-8");
        //action表示用来区分是什么操作
        String action = request.getParameter("action");

        if (action.equals("apply")) {
            apply(request, response);  //用户注册
        } else if (action.equals("AdminShowOut")) {
            AdminShowOut(request, response);
        } else if (action.equals("agreeApply")) {
            agreeApply(request, response);
        } else if (action.equals("refuseApply")) {
            refuseApply(request, response);
        } else if (action.equals("toShowApply")) {
            toShowApply(request, response);
        }
    }

    /**
     * 学生查询自己的外出申请
     * @param request
     * @param response
     */
    private void toShowApply(HttpServletRequest request, HttpServletResponse response) {
        int userId = Integer.parseInt(request.getParameter("userId"));
        OutApply outApply = new OutApply();
        outApply.setUserId(userId);

        List<OutApply> outApplies = outApplyService.getApplyByUserId(outApply);

        Map<String, Object> map = new HashMap<>();
        map.put("outApplies", outApplies);

        ResponseWriterUtils.writer(map, response);

    }

    /**
     * 驳回申请
     * @param request
     * @param response
     */
    private void refuseApply(HttpServletRequest request, HttpServletResponse response) {
        int userId = Integer.parseInt(request.getParameter("userId"));
        int OutApplyId = Integer.parseInt(request.getParameter("OutApplyId"));

        int flag = outApplyService.refuseApply(userId, OutApplyId);

        ResponseWriterUtils.writerFlag(flag, response);
    }

    /**
     * 同意出校申请
     * @param request
     * @param response
     */
    private void agreeApply(HttpServletRequest request, HttpServletResponse response) {
        int userId = Integer.parseInt(request.getParameter("userId"));
        int OutApplyId = Integer.parseInt(request.getParameter("OutApplyId"));

        int flag = outApplyService.agreeApply(userId, OutApplyId);

        ResponseWriterUtils.writerFlag(flag, response);
    }

    /**
     * 管理员查看申请
     * @param request
     * @param response
     */
    private void AdminShowOut(HttpServletRequest request, HttpServletResponse response) {
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));
        int classId = Integer.parseInt(request.getParameter("classId"));

        List<OutApply> outApplies = outApplyService.showAppliesBypage(curPage, pageSize, facultyId, classId);
        int count = outApplyService.getApplyCount(facultyId, classId);

        Map<String, Object> map = new HashMap<>();
        map.put("outApplies", outApplies);
        map.put("count", count);

        ResponseWriterUtils.writer(map, response);
    }

    /**
     * 用户进行申请
     * @param request
     * @param response
     */
    private void apply(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String identification = request.getParameter("identification");
        String httpType = request.getParameter("type");
        String date = request.getParameter("date");
        String userNumber = request.getParameter("number");
        String type;
        if (httpType.equals("true")) {
            type = "出校";
        } else {
            type = "返校";
        }
        UserService userService = new UserServiceImpl();
        User userByNumber = userService.getUserByNumber(userNumber);

        OutApply outApply = new OutApply();
        outApply.setState("未审核");
        outApply.setUserId(userByNumber.getUserId());
        outApply.setDate(date);
        outApply.setType(type);
        outApply.setUser(userByNumber);

//        Out out = new Out();
//        out.setName(name);
//        out.setIdentification(identification);
//        out.setType(type);
//        out.setDate(date);
        boolean flag = false;  //false表示不存在申请信息或者 被驳回的申请重新申请
        int i = 0;
        //调用方法
        List<OutApply> outApplies =  outApplyService.getApplyByUserId(outApply);
        if (outApplies.size() == 0) {
            i = outApplyService.addApply(outApply);
        }
        for (int j = 0; j < outApplies.size(); j++) {
            System.out.println("outApply = " + outApplies.get(j));
            if (outApplies.get(j).getType().equals(type)) {
                //类型相同，
                // 判断是否驳回，驳回则重新申请
                // 未审核 ，则申请失败
                //未审核状态
                if (outApplies.get(j).getState().equals("未审核") || outApplies.get(j).getState().equals("允许")) {
                    flag = true;
                    break;

                } else if (outApplies.get(j).getState().equals("驳回")) {
                    //申请被驳回
                    flag = false;
                    outApply.setId(outApplies.get(j).getId());
                    i = outApplyService.editApply(outApply);
                    break;
                }
            } else if (j == outApplies.size() - 1 && !outApplies.get(j).getType().equals(type)) {
                //类型不同， 可以申请
                flag = false;
                i = outApplyService.addApply(outApply);
                break;
            } else {
                continue;
            }

        }

        if (flag == false) {
            ResponseWriterUtils.writerFlag(i, response); //提交成功
        } else {
            ResponseWriterUtils.writerFlag(0, response);
        }

    }

}
